package com.ruoyi.dealrecord.service.impl;

import java.math.BigDecimal;
import java.util.*;

import com.ruoyi.agent.domain.AmAgent;
import com.ruoyi.agent.mapper.AmAgentMapper;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.dealresources.mapper.AmDealHoursingResourcesMapper;
import com.ruoyi.housingresources.domain.AmHousingResources;
import com.ruoyi.housingresources.mapper.AmHousingResourcesMapper;
import com.ruoyi.settlement.domain.AmSettlement;
import com.ruoyi.settlement.mapper.AmSettlementMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.dealrecord.mapper.AmDealRecordMapper;
import com.ruoyi.dealrecord.domain.AmDealRecord;
import com.ruoyi.dealrecord.service.IAmDealRecordService;

/**
 * 成交记录管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2020-07-29
 */
@Service
public class AmDealRecordServiceImpl implements IAmDealRecordService 
{
    @Autowired
    private AmDealRecordMapper amDealRecordMapper;

    @Autowired
    private AmHousingResourcesMapper amHousingResourcesMapper;

    @Autowired
	private AmSettlementMapper amSettlementMapper;

    @Autowired
    private AmAgentMapper amAgentMapper;

    /**
     * 查询成交记录管理
     * 
     * @param id 成交记录管理ID
     * @return 成交记录管理
     */
    @Override
    public Map<String, Object> selectAmDealRecordById(Long id)
    {
        return amDealRecordMapper.selectAmDealRecordById(id);
    }

    /**
     * 查询成交记录管理列表
     * 
     * @param amDealRecord 成交记录管理
     * @return 成交记录管理
     */
    @Override
    public List<AmDealRecord> selectAmDealRecordList(AmDealRecord amDealRecord)
    {
        Long deptId = SecurityUtils.getLoginUser().getUser().getDeptId();
        if (null != deptId){
            amDealRecord.setIpdId(deptId.toString());
        }
        return amDealRecordMapper.selectAmDealRecordList(amDealRecord);
    }

    /**
     * 新增成交记录管理
     * 
     * @param amDealRecord 成交记录管理
     * @return 结果
     */
    @Override
    public int insertAmDealRecord(AmDealRecord amDealRecord)
    {
        amDealRecord.setCreateTime(DateUtils.getNowDate());
        return amDealRecordMapper.insertAmDealRecord(amDealRecord);
    }

    /**
     * 成交记录审核
     *
     * @param id
     * @param auditStatus
     * @param reason
     * @return
     */
    @Override
    public Integer updateAuditStatus(Integer id, Integer auditStatus, String reason) {
        if (null != reason && !"undefined".equals(reason)) {
           amDealRecordMapper.updateAuditStatus(id, auditStatus, reason, null, null, null, null);
           return 1;
        }else {
            Map<String, Object> map = amDealRecordMapper.load(id);
            amDealRecordMapper.updateAuditStatus(id, auditStatus, reason, null, null, null, null);
            if (auditStatus == 4) {
                AmHousingResources amHousingResources = new AmHousingResources();
                amHousingResources.setId(((Integer) map.get("id")).longValue());
                amHousingResources.setStatus("2");
                amHousingResources.setModifiedTime(new Date());

                //通过的话 进入待支付状态
                AmSettlement amSettlement = amSettlementMapper.selectAmSettlementByDealRecordId(id);
                amSettlement.setStatus(1L);
                amSettlementMapper.updateAmSettlement(amSettlement);

                // 查看上级经纪人
                String grade = amSettlementMapper.findAgentGrade(amSettlement.getId());
                Map<String,String> mix = amSettlementMapper.getMixByGradeAndSettlementId(amSettlement.getId());
                switch (grade){
                    // 如果是C级别经纪人 的话  插入A，B级别经纪人
                    case "C":
                        amSettlement.setId(null);
                        amSettlement.setSettlementCode("JS"+DateUtils.getDate().replaceAll("-","")+new Random().nextInt(10000));
                        amSettlement.setAgentId(amAgentMapper.findAgentIdById(amSettlement.getAgentId()));
                        amSettlementMapper.insertAmSettlement(amSettlement);

                        String settlementMoney = "0.00";
                        String rebateAmount;
                        if (StringUtils.isNotNull((rebateAmount = amSettlement.getRebateAmount()))){
                            settlementMoney = (new BigDecimal(rebateAmount).multiply(new BigDecimal(mix.get("B").replace("%",""))).divide(new BigDecimal(100))).toString();
                            if (settlementMoney.contains(".")){
                                settlementMoney = (settlementMoney+"000").substring(0, settlementMoney.indexOf(".")+3 );
                            }
                        }
                        amSettlement.setMix(mix.get("B"));
                        amSettlement.setSettlementMoney(new BigDecimal(settlementMoney));
                        amSettlementMapper.updateAmSettlement(amSettlement);
                        //如果是B级别经纪人 的话  插入A级别经纪人就好了
                    case "B":
                        amSettlement.setId(null);
                        amSettlement.setSettlementCode("JS"+DateUtils.getDate().replaceAll("-","")+new Random().nextInt(10000));
                        amSettlement.setAgentId(amAgentMapper.findAgentIdById(amSettlement.getAgentId()));
                        amSettlementMapper.insertAmSettlement(amSettlement);
                        // 查询分佣占比
                         mix = amSettlementMapper.getMixByGradeAndSettlementId(amSettlement.getId());
                         settlementMoney = "0.00";
                        if (StringUtils.isNotNull((rebateAmount = amSettlement.getRebateAmount()))){
                            settlementMoney = (new BigDecimal(rebateAmount).multiply(new BigDecimal(mix.get("A").replace("%",""))).divide(new BigDecimal(100))).toString();
                            if (settlementMoney.contains(".")){
                                settlementMoney = (settlementMoney+"000").substring(0, settlementMoney.indexOf(".")+3 );
                            }
                        }
                        amSettlement.setMix(mix.get("A"));
                        amSettlement.setSettlementMoney(new BigDecimal(settlementMoney));
                        amSettlementMapper.updateAmSettlement(amSettlement);
                        break;
                        default:break;
                }



                amHousingResourcesMapper.updateAmHousingResources(amHousingResources);


                return 1;
            }
        }
        return 0;
    }

    /**
     * 查看审核情况
     */
    @Override
    public Map<String,Integer> getAuditSituation() {
        Map<String,Integer> result = new HashMap<String, Integer>(16);
        Long deptId = SecurityUtils.getLoginUser().getUser().getDeptId();
        result.put("refuse",amDealRecordMapper.getAuditSituation(3,deptId));
        result.put("auditing",amDealRecordMapper.getAuditSituation(2,deptId));
        result.put("pass",amDealRecordMapper.getAuditSituation(4,deptId));
        return result;
    }

    /**
     * 修改成交记录管理
     * 
     * @param amDealRecord 成交记录管理
     * @return 结果
     */
    @Override
    public int updateAmDealRecord(AmDealRecord amDealRecord)
    {
        return amDealRecordMapper.updateAmDealRecord(amDealRecord);
    }

    /**
     * 批量删除成交记录管理
     * 
     * @param ids 需要删除的成交记录管理ID
     * @return 结果
     */
    @Override
    public int deleteAmDealRecordByIds(Long[] ids)
    {
        return amDealRecordMapper.deleteAmDealRecordByIds(ids);
    }

    /**
     * 删除成交记录管理信息
     * 
     * @param id 成交记录管理ID
     * @return 结果
     */
    @Override
    public int deleteAmDealRecordById(Long id)
    {
        return amDealRecordMapper.deleteAmDealRecordById(id);
    }
}
